<template>
  <div class="app-container">
    <!-- *************************************搜索区************************************* -->
    <!-- 分类 -->
    <PageSearch :showSearch.sync="showSearch" :getList="getList" :handleQuery="handleQuery" :resetQuery="resetQuery">
      <template #pagination>
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
      <template #form>
        <el-form :model="queryParams" v-show="showSearch" ref="queryForm" size="small" :inline="true">
          <el-row>
            <el-form-item prop="skuNo">
              <el-input
                v-model="queryParams.skuNo"
                :placeholder="$t('sku编号')"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="isPay">
              <el-select :placeholder="$t('是否付款')" clearable @change="handleQuery" v-model="queryParams.isPay">
                <el-option :value="1" :label="$t('是')" />
                <el-option :value="0" :label="$t('否')" />
              </el-select>
            </el-form-item>
            <el-form-item prop="isAcceptance">
              <el-select
                :placeholder="$t('是否验收')"
                clearable
                @change="handleQuery"
                v-model="queryParams.isAcceptance"
              >
                <el-option :value="1" :label="$t('是')" />
                <el-option :value="0" :label="$t('否')" />
              </el-select>
            </el-form-item>

            <el-form-item prop="status">
              <el-select :placeholder="$t('租户')" v-model="queryParams.distrId" clearable @change="getList">
                <el-option v-for="item in allSellerList" :label="item.name" :value="item.distrId" />
              </el-select>
            </el-form-item>
          </el-row>
        </el-form>
      </template>
      <template #actions>
        <el-row :gutter="10">
          <el-col :span="1.5"> </el-col>
        </el-row>
      </template>
    </PageSearch>

    <el-table
      @row-click="(row) => $rowClick(row, clickedRows)"
      :row-class-name="({ row }) => $tableRowClassName(row, clickedRows)"
      @selection-change="(rows) => (tableCheck = rows)"
      :height="calTableHeight()"
      v-loading="loading"
      ref="TableRef"
      :data="tableData"
      row-key="menuId"
    >
      <!-- 未付款才能选择 -->
      <el-table-column :selectable="(row) => row.isPay == 0" type="selection" width="55" align="center" />

      <el-table-column align="center" show-overflow-tooltip prop="distrName" :label="$t('租户')" width="150" />
      <el-table-column align="center" show-overflow-tooltip prop="type" :label="$t('类型')" width="100">
        <template slot-scope="{ row }">
          <!-- type	类型 1-单品sku 2-组合sku -->
          <div v-if="row.type == 1" :style="{ color: $randomColor(row.type) }">
            {{ $t('单品sku') }}
          </div>
          <div v-if="row.type == 2" :style="{ color: $randomColor(row.type) }">
            {{ $t('组合sku') }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip prop="skuNo" :label="$t('单品sku')" width="120">
        <template slot-scope="scope">
          <div @click="handleEdit(scope.row)" style="cursor: pointer; color: #1890ff">
            {{ scope.row.skuNo }}
          </div>
        </template>
      </el-table-column>

      <el-table-column align="center" show-overflow-tooltip prop="combinationName" :label="$t('组合名')" width="120" />
      <el-table-column align="center" prop="imgUrl" :label="$t('图片')">
        <template slot-scope="scope">
          <el-image
            :key="Math.random()"
            style="width: 50px; height: 50px"
            fit="contain"
            v-hover="scope.row.imgUrl"
            :src="scope.row.imgUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip prop="skuNameCn" :label="$t('名字')" width="150" />
      <el-table-column align="center" show-overflow-tooltip prop="isPay" :label="$t('是否付款')" width="80">
        <!-- isPay	是否付款 0-否 1-是 -->
        <template #default="{ row }">
          <div v-if="row.isPay == 0" style="color: red">{{ $t('否') }}</div>
          <div v-if="row.isPay == 1" style="color: green">{{ $t('是') }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip prop="isAcceptance" :label="$t('是否验收')" width="80">
        <!-- isAcceptance	是否验收 0-否 1-是 -->
        <template #default="{ row }">
          <div v-if="row.isAcceptance == 0" style="color: red">{{ $t('否') }}</div>
          <div v-if="row.isAcceptance == 1" style="color: green">{{ $t('是') }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip prop="num" :label="$t('申请数量')" />
      <el-table-column align="center" show-overflow-tooltip prop="completeStatus" :label="$t('模板完成数量')" />
      <el-table-column align="center" show-overflow-tooltip prop="createTime" :label="$t('申请时间')" width="150" />
      <el-table-column align="center" show-overflow-tooltip prop="updateTime" :label="$t('更新时间')" width="150" />

      <el-table-column fixed="right" :label="$t('操作')" align="center" width="100">
        <template slot-scope="{ row }">
          <el-button
            v-show="row.isPay == 1"
            v-hasPermi="['distribution:skuTempProxy:getSkuProxyTemplateList']"
            type="text"
            @click="handleTemplate(row)"
          >
            {{ $t('查看模板') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pageTotal > 0"
      :total="pageTotal"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加文案代做-单品 -->
    <Template
      :show.sync="template.show"
      :skuId="template.formData.skuId"
      @getList="getList"
      :formData="template.formData"
    />
    <!-- 添加文案代做-组合 -->
    <TemplateCombination
      :show.sync="templateCombination.show"
      :skuId="templateCombination.formData.skuId"
      @getList="getList"
      :formData="templateCombination.formData"
    />

    <!-- 支付弹框 支付详情-->
    <Pay :show.sync="pay.show" :formData="pay.formData" @getList="getList" @updataPayType="updataPayType">
      <template #default="{ handlePayMethod }">
        <div style="margin: 20px 0" v-hasPermi="['distributor:distributorPoints:sellerPointsPayOffline']">
          线下支付：
        </div>
        <div class="payContentOffline" v-hasPermi="['distributor:distributorPoints:sellerPointsPayOffline']">
          <el-button
            :class="{ 'is-active': offlinePayTypestr == 1 }"
            @click="offlinePayType(1, handlePayMethod, 'offlineCash')"
          >
            现金支付
          </el-button>
          <el-button
            :class="{ 'is-active': offlinePayTypestr == 2 }"
            @click="offlinePayType(2, handlePayMethod, 'offlineTransfer')"
          >
            银行卡转账
          </el-button>
        </div>
        <!-- <div style="display: flex; justify-content: center; margin-top: 20px">
          <el-button type="primary" @click="confirmPayment" v-hasPermi="['system:skuSellerApply:handBeforePay']">
            {{ $t('确认支付') }}</el-button
          >
        </div> -->
      </template>
    </Pay>
    <!-- sku详情/修改 -->
    <Skucontmoduler @relaod="relaod" ref="Skucontmoduler"></Skucontmoduler>
  </div>
</template>

<script>
import {
  list,
  templateProxyPay,
  skuTempProxyApplyDelete,
  sellerPointsPayOffline,
  getAllSellerList,
} from '@/api/distribution/sku/skuTempProxyApply'
import Pay from '@/components/Pay/index.vue'

import Skucontmoduler from '@/components/Skucontmoduler'
import Template from './components/template.vue'
import TemplateCombination from './components/templateCombination.vue'

import { listSkuClass } from '@/api/platform/goodsClassDict'
export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  name: 'SellerSku',

  components: {
    Skucontmoduler,
    Template,
    TemplateCombination,
    Pay,
  },
  data() {
    return {
      offlinePayTypestr: '', //支付方式

      allSellerList: [],
      tableCheck: [],
      /**点击的表格项 */
      clickedRows: [],
      tableData: [],
      /**pay */
      pay: {
        show: false,
        type: '',
        formData: {},
        idList: [],
      },
      /**添加文案代做 */
      template: {
        show: false,
        formData: {},
      },
      /**添加文案代做-组合 */
      templateCombination: {
        show: false,
        formData: {},
      },

      showSearch: false,
      /**数据总条数 */
      pageTotal: 0,
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  watch: {
    'queryParams.pageNum': function (val) {
      this.clickedRows = []
    },
  },
  created() {
    this.getList()
    /** 查询菜单下拉树结构 */
    listSkuClass().then((response) => {
      this.menuOptions = []
      this.menuOptions = this.handleTree(response.data, 'id')
    })

    getAllSellerList().then((res) => {
      this.allSellerList = res.data
    })
  },
  methods: {
    handleDelete(row) {
      this.$confirm(this.$t('确认删除?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        skuTempProxyApplyDelete(row.id).then((res) => {
          const { code, msg } = res
          if (code === 200) {
            this.$message({ type: 'success', message: msg })
            this.getList()
          }
        })
      })
    },
    /**线下支付方式 */
    offlinePayType(offlinePayType, handlePayMethod, payTypeStr) {
      this.offlinePayTypestr = offlinePayType
      handlePayMethod({ payChannel: payTypeStr })
    },
    updataPayType(selectPayChannel) {
      if (selectPayChannel) {
        this.offlinePayTypestr = selectPayChannel == 'offlineCash' ? 1 : 2
        let that = this
        this.$modal
          .confirm(
            `是否确认${this.offlinePayTypestr == 1 ? '现金支付' : this.offlinePayTypestr == 2 ? '银行卡转账' : ''}支付`,
          )
          .then(() => {
            sellerPointsPayOffline({
              id: that.pay.formData.businessId,
              offlinePayType: that.offlinePayTypestr,
            }).then(() => {
              this.$message({ type: 'success', message: '操作成功' })
              // this.cancel()
              // this.$emit('getList')
              this.getList()
              this.pay.show = false
            })
          })
      } else {
        this.offlinePayTypestr = ''
      }
    },
    handleTemplate(row) {
      // <!-- type	类型 1-单品sku 2-组合sku -->
      if (row.type == 1) {
        this.template.formData = row
        this.template.show = true
      } else {
        this.templateCombination.formData = row
        this.templateCombination.show = true
      }
    },
    handlePay(type, _data) {
      const data = {
        idList: type == 'batch' ? _data.map((i) => i.id) : [_data.id],
      }
      templateProxyPay(data).then((res) => {
        const { code, data, msg } = res
        if (code === 200) {
          this.pay.formData = data
          this.pay.type = type
          this.pay.show = true
        }
      })
    },

    relaod() {
      this.getList()
    },
    handleEdit(row) {
      this.$refs.Skucontmoduler.setSkuInfo(row)
      this.$refs.Skucontmoduler.checkNumber('basicInformationSeller')
    },
    submitCloseLimit(value) {
      closeLimit({
        id: value.id,
      }).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: 'success',
            message: res.msg,
          })
        }
        this.getList()
      })
    },
    // 取消申请
    cancel() {
      this.editDialogConfig = !this.editDialogConfig
    },

    /** 查询菜单列表 */
    getList() {
      this.loading = true
      list(this.queryParams).then((res) => {
        this.tableData = res.rows
        this.pageTotal = res.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.value1 = null
      Object.keys(this.queryParams).forEach((key) => {
        this.queryParams[key] = null
      })
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10

      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped>
.goods {
  &-img {
    &-box {
      position: relative;
    }

    &-mini {
      height: 50px;
      border-radius: 3px;

      &:hover {
        & + .goods-img-big {
          display: block;
        }
      }
    }

    &-big {
      // display: none;
      position: absolute;
      z-index: 100;
      top: -180px;
      left: 0;
      transform: translate(0, 70px);
      width: 200px;
      height: 200px;
      border-radius: 3px;
    }
  }
}

.bigImg {
  border-radius: 3px;
  z-index: 10;
  position: fixed;
  width: 200px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>
